package com.example.statistics.ui.product

import android.os.Bundle
import android.view.View
import com.example.statistics.R
import com.example.statistics.bean.ProductBean
import com.example.statistics.common.BaseLoadingListActivity
import com.example.statistics.ex.imageDisplay
import com.example.statistics.ex.messageDialog
import com.example.statistics.net.UrlConfig
import com.example.statistics.net.callback.DataDialogHttpCallback
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.example.statistics.utils.activityForResult
import com.jhj.httplibrary.HttpCall
import com.jhj.slimadapter.SlimAdapter
import com.jhj.slimadapter.holder.ViewInjector
import com.example.statistics.ex.glideRound
import org.jetbrains.anko.startActivity

class ProductInfoListActivity : BaseLoadingListActivity<ProductBean>() {

    var action: String? = null

    override val title: String
        get() = "商品列表"

    override val url: String
        get() = UrlConfig.PRODUCT_INFO_LIST

    override val itemLayoutRes: Int
        get() = R.layout.list_item_product_info

    override val inputSearch: Boolean
        get() = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        action = intent.getStringExtra(Constants.ACTION)
        topBarRightImage {
            startActivity<ProductEditActivity>(Constants.TYPE to "猪副")
        }
    }

    override fun itemViewConvert(adapter: SlimAdapter, injector: ViewInjector, bean: ProductBean, position: Int) {
        val imageList = bean.imageList?.map { UrlConfig.BASE_URL + it.path }
        injector.text(R.id.tv_title, bean.factoryNo + bean.productName)
                .text(R.id.tv_status, bean.productNo)
                .visibility(R.id.tv_status, if (bean.productNo.isEmpty()) View.GONE else View.VISIBLE)
                .glideRound(R.id.iv_product, if (imageList.isNullOrEmpty()) "" else imageList[0])
                .clicked {
                    if (!action.isNullOrBlank()) {
                        activityFinish(Constants.DATA to bean)
                    } else {
                        activityForResult<ProductInfoActivity>(Constants.ID to bean.id, Constants.TYPE to "猪副") {
                            refresh()
                        }
                    }
                }
                .clicked(R.id.iv_product) {
                    imageDisplay(imageList.orEmpty())
                }
                .longClicked {
                    messageDialog(msg = "是否删除“${bean.factoryNo + bean.productName}”?") { alertFragment, view ->
                        HttpCall.get(UrlConfig.PRODUCT_INFO_DELETE)
                                .addParam("id", bean.id)
                                .enqueue(object : DataDialogHttpCallback<Int>(this) {
                                    override fun onSuccess(data: Int?, resultType: ResultType) {
                                        refresh()
                                    }
                                })
                    }
                    false
                }
    }


}